﻿using UnityEngine;
using System.Collections;

public class TabBarButtonEx : MonoBehaviour
{
	Transform m_On;
	Transform m_Off;
	Transform m_Disable;
	
	TabBarEx m_tabBar = null;
	public TabBarEx TabBar
	{
		get { return m_tabBar; }
		set { m_tabBar = value; }
	}
	
	int m_buttonIndex = 0;
	public int ButtonIndex
	{
		get { return m_buttonIndex; }
		set { m_buttonIndex = value; }
	}
	
	bool m_IsChecked = false;
	public bool IsChecked
	{
		get { return m_IsChecked; }
		set { m_IsChecked = value; SetState(); OnSelected(); }
	}
	
	public bool isEnabled 
	{ 
		get { return enabled; } 
		set { enabled = value; SetDisable(); } 
	}
	
	void Awake()
	{
		m_On = transform.Find("on");
		m_Off = transform.Find("off");       
		m_Disable = transform.Find("disable");
		
		SetState();
	}

	void OnPress(bool isPressed)
	{
		if (isEnabled == true && IsChecked == false)
		{
			TabBar.SelectTab(ButtonIndex);
		}
	}
	
	void SetDisable()
	{
		if (m_Disable	!= null)	m_Disable.gameObject.SetActive(!isEnabled);
		if (m_On		!= null)	m_On.gameObject.SetActive(isEnabled);
		if (m_Off		!= null)	m_Off.gameObject.SetActive(isEnabled);     
	}
	
	void SetState()
	{
		m_Off.gameObject.SetActive(!IsChecked);
		m_On.gameObject.SetActive(IsChecked);
	}
	
	protected virtual void OnSelected()
	{
		// Need Implementation		
	}
}
